package com.kylin.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.kylin.handler.GuliException;
import com.kylin.vod.service.VodService;
import com.kylin.vod.utils.ConstantVodUtils;
import com.kylin.vod.utils.InitVodClient;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class VodServiceImpl implements VodService {


    //上传视频到阿里云
    @Override
    public String uploadVideoAly(MultipartFile file) {
        String accessKeyId = ConstantVodUtils.ACCESS_KEY_ID;
        String accessKeySecret = ConstantVodUtils.ACCESS_KEY_SECRET;
        InputStream inputStream = null;
        String fileName = file.getOriginalFilename();//文件原始名称
        //只要fileName的名称不要.xxx后缀
        String title = fileName.substring(0, fileName.lastIndexOf("."));//上传之后显示名称
        //视频Id
        String videoId = null;
        try {
            System.out.println(accessKeyId);
            System.out.println(accessKeySecret);
            System.out.println(title);
            System.out.println(fileName);
            inputStream = file.getInputStream();
            //title上传之后显示名称 fileName文件原始名称
            UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            if (response.isSuccess()) {
                System.out.print("VideoId=" + response.getVideoId() + "\n");
                videoId = response.getVideoId();
            } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
                videoId = response.getVideoId();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return videoId;
    }

    //删除多个阿里云视频的方法
    @Override
    public void removeMoreAlyVideo(List videoIdList) {
        //初始化对象
        try {
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建删除视频的request对象
            DeleteVideoRequest request = new DeleteVideoRequest();

            //videoIdList 使用apache.commons.lang.StringUtils,将list中所有的值用','拼接成一个字符串
            String videIds = StringUtils.join(videoIdList.toArray(), ",");

            System.out.println(videIds);
            //支持传入多个视频ID，多个用逗号分隔 "VideoId1,VideoId2"
            request.setVideoIds(videIds);
            //调用初始化对象的方法实现删除
            client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            throw new GuliException(20001,"视频删除失败!");
        }
    }

}
